package com.bci.pwtz.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import com.bci.pwtz.common.util.PwtzUtils;
import com.bci.pwtz.exception.PwtzDatabaseException;
import com.bci.pwtz.mysql.dao.VzystatMapper;
import com.bci.pwtz.mysql.model.Vzystat;
import com.bci.pwtz.service.CompanyService;
import com.bci.pwtz.service.VzystatService;

/**
 * 一句话描述 <br/>
 * <p>
 * Description:<br/>
 * <p>
 * TODO 详细描述
 * 
 * <p>
 * For Examples <br/>
 * 
 * <PRE>
 *      TODO 代码使用示例
 * </PRE>
 * <p>
 */
@Service
public class VzystatServiceImpl implements VzystatService
{

    
    private VzystatMapper objMapper;
    
    private CompanyService companyService;
    
    /**
     * @return the objMapper
     */
    public VzystatMapper getObjMapper()
    {
        return objMapper;
    }

    /**
     * @param objMapper the objMapper to set
     */
    public void setObjMapper(VzystatMapper objMapper)
    {
        this.objMapper = objMapper;
    }

    /**
     * @return the companyService
     */
    public CompanyService getCompanyService()
    {
        return companyService;
    }

    /**
     * @param companyService the companyService to set
     */
    public void setCompanyService(CompanyService companyService)
    {
        this.companyService = companyService;
    }

    private static final Logger _logger = Logger.getLogger(VzystatServiceImpl.class);
    
    @Override
    public List<Vzystat> findListByUserId(Long companyId,int type) throws PwtzDatabaseException
    {
        if (_logger.isInfoEnabled())
        {
            _logger.info("findVzystat:|companyId=" + companyId);
        }
        List<Vzystat> list = null;
        List<Vzystat> newlist = new ArrayList<Vzystat>();
        String companyidStr = "";
        String[] companyids=null;
//        if(type==1){
            companyidStr = companyService.findChildrensId(companyId,null);
            if(companyidStr!=null && companyidStr.length()>0){
                companyidStr= companyidStr.substring(0,companyidStr.length()-1);
                companyids =companyidStr.split(",");
            }
//        }
        try
        {
            list = objMapper.selectListByUserId(companyId, companyids);
            Vzystat vzs= null;
            if(list.size()>0){
                if(type==1){
                   String cids=companyService.findChildren(companyId);
                   String[] ids =null;
                   if(cids!=null)
                       ids = cids.split(",");
                   for(String id:ids){
                       String cids1=companyService.findChildrensId(PwtzUtils.passLong(id,-1),null);
                       String[] ids1 =null;
                       if(cids1!=null)
                           ids1 = cids1.split(",");
                       vzs = findList(list,ids1);
                       if(vzs!=null && vzs.getCompanyId()!=null)
                       newlist.add(vzs);
                   }
                   //统计本单位数据进统计
                   //vzs = findList(list,null);
                   //newlist.add(vzs);
                   for(Vzystat v:list){
                       if(companyId==v.getCompanyId()){
                           newlist.add(v);
                       }
                   }
                   
                }else if(type==0){
                    vzs = findList(list,null);
                    if(vzs!=null && vzs.getCompanyId()!=null)
                    newlist.add(vzs);
                }
            }
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findVzystat:result list=" + newlist.toString());
        }

        return newlist;
    }
    
    private Vzystat findList(List<Vzystat> list,String[] ids) throws PwtzDatabaseException
    {
        Vzystat vzs= new Vzystat();;
        try
        {
            if(list.size()>0){
                Long   allzyxlmainlinecount          =0L; 
                Long   allzyxlsublinecount           =0L;
                Double allzyxltotallinelength        =0D;
                Double allzyxloverheadlinelength     =0D;
                Double allzyxloverheadlinelengthBar  =0D;
                Double allzyxloverheadlinelengthCover=0D;
                Double allzyxlcablelength            =0D;
                Long   allzygtcount                  =0L;
                Long   alllxcount                    =0L;
                Long   allpublicbyqcount             =0L;
                Double allpublicbyqcapacity          =0D;
                Long   allprivatebyqcount            =0L;
                Double allprivatebyqcapacity         =0D;
                Long   allzykgzcount                 =0L;
                Long   allzypdscount                 =0L;
                Long   allzyhwgcount                 =0L;
                Long   allzyxsbdzcount               =0L;
                Long   allzydlfzxcount               =0L;
                Long   allzyzsdlqcount               =0L;
                Long   allzyzsfhkgcount              =0L;
                Long   allzyzsglkgcount              =0L;
                Long   allzyzschqcount               =0L;
                Long   allzyzsdlsrdqcount            =0L;
                Long   allzyzsblqcount               =0L;
                Long   allzyzsdrqcount               =0L;
                Double allzyzsdrqcapacity            =0D;
                int count=0;
                for(int i=0;i<list.size();i++){
                    if(ids==null){
                        vzs.setCompanyName(list.get(0).getCompanyName());
                        allzyxlmainlinecount           +=list.get(i).getZyxlmainlinecount()          ; 
                        allzyxlsublinecount            +=list.get(i).getZyxlsublinecount()           ;
                        allzyxltotallinelength         +=list.get(i).getZyxltotallinelength()        ;
                        allzyxloverheadlinelength      +=list.get(i).getZyxloverheadlinelength()     ;
                        allzyxloverheadlinelengthBar   +=list.get(i).getZyxloverheadlinelengthBar()  ;
                        allzyxloverheadlinelengthCover +=list.get(i).getZyxloverheadlinelengthCover();
                        allzyxlcablelength             +=list.get(i).getZyxlcablelength()            ;
                        allzygtcount                   +=list.get(i).getZygtcount()                  ;
                        alllxcount                     +=list.get(i).getLxcount()                    ;
                        allpublicbyqcount              +=list.get(i).getPublicbyqcount()             ;
                        allpublicbyqcapacity           +=list.get(i).getPublicbyqcapacity()          ;
                        allprivatebyqcount             +=list.get(i).getPrivatebyqcount()            ;
                        allprivatebyqcapacity          +=list.get(i).getPrivatebyqcapacity()         ;
                        allzykgzcount                  +=list.get(i).getZykgzcount()                 ;
                        allzypdscount                  +=list.get(i).getZypdscount()                 ;
                        allzyhwgcount                  +=list.get(i).getZyhwgcount()                 ;
                        allzyxsbdzcount                +=list.get(i).getZyxsbdzcount()               ;
                        allzydlfzxcount                +=list.get(i).getZydlfzxcount()               ;
                        allzyzsdlqcount                +=list.get(i).getZyzsdlqcount()               ;
                        allzyzsfhkgcount               +=list.get(i).getZyzsfhkgcount()              ;
                        allzyzsglkgcount               +=list.get(i).getZyzsglkgcount()              ;
                        allzyzschqcount                +=list.get(i).getZyzschqcount()               ;
                        allzyzsdlsrdqcount             +=list.get(i).getZyzsdlsrdqcount()            ;
                        allzyzsblqcount                +=list.get(i).getZyzsblqcount()               ;
                        allzyzsdrqcount                +=list.get(i).getZyzsdrqcount()               ;
                        allzyzsdrqcapacity             +=list.get(i).getZyzsdrqcapacity()            ;
                    }else{
                        for(String id:ids){
                            if(id.equals(list.get(i).getCompanyId().toString())){
                                if(count ==0){
                                    vzs.setCompanyName(list.get(i).getCompanyName());
                                }
                                count ++;
                                allzyxlmainlinecount           +=list.get(i).getZyxlmainlinecount()          ; 
                                allzyxlsublinecount            +=list.get(i).getZyxlsublinecount()           ;
                                allzyxltotallinelength         +=list.get(i).getZyxltotallinelength()        ;
                                allzyxloverheadlinelength      +=list.get(i).getZyxloverheadlinelength()     ;
                                allzyxloverheadlinelengthBar   +=list.get(i).getZyxloverheadlinelengthBar()  ;
                                allzyxloverheadlinelengthCover +=list.get(i).getZyxloverheadlinelengthCover();
                                allzyxlcablelength             +=list.get(i).getZyxlcablelength()            ;
                                allzygtcount                   +=list.get(i).getZygtcount()                  ;
                                alllxcount                     +=list.get(i).getLxcount()                    ;
                                allpublicbyqcount              +=list.get(i).getPublicbyqcount()             ;
                                allpublicbyqcapacity           +=list.get(i).getPublicbyqcapacity()          ;
                                allprivatebyqcount             +=list.get(i).getPrivatebyqcount()            ;
                                allprivatebyqcapacity          +=list.get(i).getPrivatebyqcapacity()         ;
                                allzykgzcount                  +=list.get(i).getZykgzcount()                 ;
                                allzypdscount                  +=list.get(i).getZypdscount()                 ;
                                allzyhwgcount                  +=list.get(i).getZyhwgcount()                 ;
                                allzyxsbdzcount                +=list.get(i).getZyxsbdzcount()               ;
                                allzydlfzxcount                +=list.get(i).getZydlfzxcount()               ;
                                allzyzsdlqcount                +=list.get(i).getZyzsdlqcount()               ;
                                allzyzsfhkgcount               +=list.get(i).getZyzsfhkgcount()              ;
                                allzyzsglkgcount               +=list.get(i).getZyzsglkgcount()              ;
                                allzyzschqcount                +=list.get(i).getZyzschqcount()               ;
                                allzyzsdlsrdqcount             +=list.get(i).getZyzsdlsrdqcount()            ;
                                allzyzsblqcount                +=list.get(i).getZyzsblqcount()               ;
                                allzyzsdrqcount                +=list.get(i).getZyzsdrqcount()               ;
                                allzyzsdrqcapacity             +=list.get(i).getZyzsdrqcapacity()            ;
                            }
                        }
                    }
                }
                
                vzs.setZyxlmainlinecount(allzyxlmainlinecount          );
                vzs.setZyxlsublinecount(allzyxlsublinecount           );
                vzs.setZyxltotallinelength(allzyxltotallinelength        );
                vzs.setZyxloverheadlinelength(allzyxloverheadlinelength     );
                vzs.setZyxloverheadlinelengthBar(allzyxloverheadlinelengthBar  );
                vzs.setZyxloverheadlinelengthCover(allzyxloverheadlinelengthCover);
                vzs.setZyxlcablelength(allzyxlcablelength            );
                vzs.setZygtcount(allzygtcount                  );
                vzs.setLxcount(alllxcount                    );
                vzs.setPublicbyqcount(allpublicbyqcount             );
                vzs.setPublicbyqcapacity(allpublicbyqcapacity          );
                vzs.setPrivatebyqcount(allprivatebyqcount            );
                vzs.setPrivatebyqcapacity(allprivatebyqcapacity         );
                vzs.setZykgzcount(allzykgzcount                 );
                vzs.setZypdscount(allzypdscount                 );
                vzs.setZyhwgcount(allzyhwgcount                 );
                vzs.setZyxsbdzcount(allzyxsbdzcount               );
                vzs.setZydlfzxcount(allzydlfzxcount               );
                vzs.setZyzsdlqcount(allzyzsdlqcount               );
                vzs.setZyzsfhkgcount(allzyzsfhkgcount              );
                vzs.setZyzsglkgcount(allzyzsglkgcount              );
                vzs.setZyzschqcount(allzyzschqcount               );
                vzs.setZyzsdlsrdqcount(allzyzsdlsrdqcount            );
                vzs.setZyzsblqcount(allzyzsblqcount               );
                vzs.setZyzsdrqcount(allzyzsdrqcount               );
                vzs.setZyzsdrqcapacity(allzyzsdrqcapacity            );

            }
            
        }
        catch (DataAccessException e)
        {
            _logger.error(ExceptionUtils.getStackTrace(e));
            throw new PwtzDatabaseException();
        }
        if (list!=null)
        {
            _logger.debug("findVdystat:result list=" + list.toString());
        }

        return vzs;
    }
}
